#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
       ui->setupUi(this);
    // QLineSeries* s1=new QLineSeries(this);//折线图
    // QLineSeries* s2=new QLineSeries(this);
       QSplineSeries*s1=new QSplineSeries();//曲线图
       QSplineSeries*s2=new QSplineSeries();
    s2->setName("美国");
    s1->setColor(Qt::red);
    s2->setColor(Qt::blue);

    s1->append(2018,1000);
    s1->append(2019,1200);
    s1->append(2020,1500);
    s1->append(2021,2000);
    s1->append(2022,2300);

    s2->append(2018,1200);
    s2->append(2019,1500);
    s2->append(2020,2000);
    s2->append(2021,1500);
    s2->append(2022,3000);
    QChart*chart=new QChart;
    chart->setTitle("模拟军方开支");
    chart->addSeries(s1);
    chart->addSeries(s2);

    QValueAxis *X=new QValueAxis;
    X->setRange(2018,2022);//设置坐标轴的刻度
     X->setTickCount(5);//设置几个刻度
    X->setTitleText("年份");

    QValueAxis *Y=new QValueAxis;
    Y->setRange(1000,4000);
    Y->setTickInterval(500);
    Y->setTickCount(6);
   // Y->setMinorTickCount(500);
    Y->setTitleText("开支（单位：万亿）");

    // chart->setAxisX(X,s1);
    // chart->setAxisY(Y,s1);
    // chart->setAxisX(X,s2);
    // chart->setAxisY(Y,s2);
    //常用的方法
    chart->addAxis(X,Qt::AlignBottom);
    chart->addAxis(Y,Qt::AlignLeft);
    // 将坐标轴关联到序列
    s1->attachAxis(X);
    s1->attachAxis(Y);
    s2->attachAxis(X);
    s2->attachAxis(Y);
    QChartView* view=new QChartView(this);
    setCentralWidget(view);
    view->setChart(chart);





}

MainWindow::~MainWindow()
{
    delete ui;
}
